package com.example.sqlite3;


import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author jim
 */
public class MainActivity extends AppCompatActivity {
    /**
     * 引入数据库助手类
     */
    private MyDatebaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatebaseHelper(this,"BookStore.db",null,2);
        // 初始化创建按钮
        Button createDatabase = (Button) findViewById(R.id.create_database);
        // 设置点击事件
        createDatabase.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // 执行写入数据库操作
                dbHelper.getWritableDatabase();
            }
        });

        // 初始化添加数据按钮
        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                // 开始组装第一条数据
                values.put("name","The Da Vinci Code");
                values.put("author","Dan Brown");
                values.put("pages",454);
                values.put("price",16.96);
                // 插入第一条数据
                db.insert("Book",null,values);

                // 开始组装第二条数据
                values.put("name","The Lost Symbol");
                values.put("author","Dan Brown");
                values.put("pages",510);
                values.put("price",19.95);
                // 插入第二条数据
                db.insert("Book",null,values);
            }
        });

// 更改数据
        Button updateData = (Button) findViewById(R.id.update_data);
        updateData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();

                values.put("price",18.95);

                // 修改数据
                db.update("Book",values,"id = ?",new String[] {"1"});
            }
        });

    // 删除数据
        Button deleteData =  findViewById(R.id.delete_data);
        deleteData.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();

            // 删除数据
            db.delete("Book","pages > ?",new String[] {"500"});
        }
    });

        // 查询数据
        Button queryData = (Button) findViewById(R.id.query_data);
        queryData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                // 查询Book表中所有的数据
                Cursor cursor = db.query("Book",null,null,null,null,null,null);
                if (cursor.moveToFirst()) {
                    do {
                        // 遍历Cursor对象，取出数据
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));

                        Log.d("---","book name is "+name);
                        Log.d("---","book author is "+author);
                        Log.d("---","book pages is "+pages);
                        Log.d("---","book price is "+price);
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        });

    }
}